package com.atguigu.product.products.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.product.products.entity.PmsAttrAttrgroupRelationEntity;
import com.atguigu.product.products.entity.PmsAttrEntity;
import com.atguigu.product.products.service.PmsCategoryService;
import com.atguigu.product.products.vo.respVo.CatelogwithattrRespVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.product.products.entity.PmsAttrGroupEntity;
import com.atguigu.product.products.service.PmsAttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 属性分组
 *
 * @author qhw
 * @email qhw@gmail.com
 * @date 2021-08-03 19:57:10
 */
@RestController
@RequestMapping("product/attrgroup")
public class PmsAttrGroupController {
    @Autowired
    private PmsAttrGroupService pmsAttrGroupService;
    @Autowired
    private PmsCategoryService pmsCategoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("products:pmsattrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = pmsAttrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取分类下所有分组&关联属性
     */
    @RequestMapping("/{catelogId}/withattr")
    //@RequiresPermissions("products:pmsattrgroup:list")
    public R catelogIdWithattr(@PathVariable("catelogId") Long catelogId) {
        List<CatelogwithattrRespVo> catelogwithattrRespVos = pmsAttrGroupService.catelogIdWithattr(catelogId);
        if (catelogwithattrRespVos == null) {
            return R.error().put("data", "没有找到对应的分组信息");
        }
        return R.ok().put("data", catelogwithattrRespVos);
    }

    /**
     * 根据分组id查询关联关系
     */
    @RequestMapping("/{groupId}/attr/relation")
    // @RequiresPermissions("products:pmsattrgroup:list")
    public R attrRelation(@PathVariable("groupId") Long groupId) {
        List<PmsAttrEntity> pmsAttrEntities = pmsAttrGroupService.attrRelation(groupId);

        return R.ok().put("data", pmsAttrEntities);
    }

    /**
     * 根据id查看分组信息
     */
    @RequestMapping("/list/{categoryId}")
    public R queryCategoryId(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId) {
        PageUtils page = pmsAttrGroupService.queryCategoryId(params, categoryId);

        return R.ok().put("page", page);
    }

    /**
     * 1430748135004680193/noattr/relation
     * 查询没有分组的属性信息
     */
    @RequestMapping("/{groupId}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params, @PathVariable("groupId") Long groupId) {
        PageUtils page = pmsAttrGroupService.noattrRelation(params, groupId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        PmsAttrGroupEntity pmsAttrGroup = pmsAttrGroupService.getById(attrGroupId);
        //根据层级id查询递进关系
        Long[] categorys = pmsCategoryService.queryRec(pmsAttrGroup.getCatelogId());
        pmsAttrGroup.setCatelogPath(categorys);
        return R.ok().put("attrGroup", pmsAttrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("products:pmsattrgroup:save")
    public R save(@RequestBody PmsAttrGroupEntity pmsAttrGroup) {
        pmsAttrGroupService.save(pmsAttrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("products:pmsattrgroup:update")
    public R update(@RequestBody PmsAttrGroupEntity pmsAttrGroup) {
        pmsAttrGroupService.updateById(pmsAttrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("products:pmsattrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        pmsAttrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    /**
     * 新增分组关联属性
     */
    @RequestMapping("/attr/relation")
    // @RequiresPermissions("products:pmsattrgroup:list")
    public R attrRelation1(@RequestBody List<PmsAttrAttrgroupRelationEntity> entities) {
        pmsAttrGroupService.attrRelationDetail(entities);

        return R.ok();
    }

}
